// pages/member/people/list/index.js
import {
  request
} from '../../../../request';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    memberList: [],
    modalName: null,
    applyTime:12,
    selectItem:null
  },
  onShow: function () {
    this.initData();
  },
  initData() {
    request("/gym/member/list", "GET").then(resopnse => {
      console.log(resopnse)
      this.setData({
        memberList: resopnse.rows
      });
    })
  },
  toForm(e) {
    if (e.currentTarget.dataset.target == "add") {
      wx.navigateTo({
        url: '/pages/member/people/form/index',
      })
    }
  },
  pageBack() {
    wx.navigateBack({
      delta: 1
    });
  }, // ListTouch触摸开始
  ListTouchStart(e) {
    this.setData({
      ListTouchStart: e.touches[0].pageX
    })
  },

  // ListTouch计算方向
  ListTouchMove(e) {
    this.setData({
      ListTouchDirection: e.touches[0].pageX - this.data.ListTouchStart > 0 ? 'right' : 'left'
    })
  },

  // ListTouch计算滚动
  ListTouchEnd(e) {
    if (this.data.ListTouchDirection == 'left') {
      this.setData({
        modalName: e.currentTarget.dataset.target
      })
    } else {
      this.setData({
        modalName: null
      })
    }
    this.setData({
      ListTouchDirection: null
    })
  },
  delete(e) {
    var that = this;
    var item = e.currentTarget.dataset['item'];
    wx.showModal({
      title: '提示',
      content: '是否删除当前用户',
      success(res) {
        if (res.confirm) {
          request("/gym/member/" + item.memberId, "DELETE").then(res => {
            that.initData();
            if (res.code == "200") {
              wx.showToast({
                title: '删除成功',
                icon: '',
                duration: 2000,
              })
              
            } else {
              wx.showToast({
                title: res.msg,
                icon: 'none',
                duration: 2000,
              })
            }
          })
          
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  modify(e) {
    var item = e.currentTarget.dataset['item'];
    wx.navigateTo({
      url: '/pages/member/people/form/index?id=' + item.memberId,
    })
  },
  showModal(e) {
    console.log(e);
    this.setData({
      modalName: e.currentTarget.dataset.target,
      selectItem:e.currentTarget.dataset.item
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null,
      selectItem:null
    })
  },
  onChange(e) {
    this.setData({
      applyTime: e.detail.value
    })
    console.log(this.data);
  },
  applyCard(){
    var that = this;
    request("/gym/member/apply/" + this.data.selectItem.memberId+"/"+this.data.applyTime, "GET").then(res => {
      if (res.code == "200") {
        that.setData({
          modalName: null,
          selectItem:null
        })
        that.initData();
        wx.showToast({
          title: '开卡成功',
          icon: '',
          duration: 2000,
        })
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none',
          duration: 2000,
        })
      }
    })
  },
})